<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/database.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/profile.php';

	//Get data
	$start = 0;
	if (isset($_GET['start'])) $start = $_GET['start'];

	//Prepare variable
	$posts = array();

	//Load wall posts
	$result = mysql_query("SELECT * FROM wall_posts WHERE user_id=" . $user['id'] . " ORDER BY time DESC") or die (mysql_error());
	$row	= mysql_fetch_array($result);
	$index	= 0;
	$amount	= mysql_num_rows($result);
	
	//Get pages
	$page = 1;
	if (isset($_GET['page'])) $page = $_GET['page'];	

	$pages = ceil($amount/$walls_in_page);
	$index = $walls_in_page * ($page - 1);
	$amount = $index + $walls_in_page;

	$result = mysql_query("SELECT * FROM wall_posts WHERE user_id=" . $user['id'] . " ORDER BY time DESC LIMIT $index, $amount") or die (mysql_error());
	$row	= mysql_fetch_array($result);
	
	//reset
	$index = 0;
	$amount = $walls_in_page;
	
	//While
	while ($row && count($posts) < $amount) {
		//Starting from start
		if  ($index >= $start) {
			//Prepare variable
			$i			= 0;
			$wall_post	= "";
			$tag_found	= false;

			//For each char
			for ($i = 0; $i < strlen($row['content']); $i++) {
				//$text = $row['content'];
				$char = substr($row['content'], $i, 1);
				//echo $char;
				if ($char == '@') {
					//if already tagging
					if ($tag_found) {
						//Get character ID
						$user_result = mysql_query("SELECT id FROM users WHERE username='$username'");

						//If data exist
						if (mysql_num_rows($user_result) > 0) {
							//Create link
							$user_row = mysql_fetch_array($user_result);
							$username = '@<a href="index.php?id=' . $user_row['id'] . '">' . $username . "<a/>";
						} else {
							//Just leave it as usual
							$username = '@' . $username;
						}
						
						//Append
						$wall_post	.= $username;
					}

					//Start tagging
					$tag_found	= true;
					$username	= "";
				} else if ($char == ' ' && $tag_found) {
					//Tagging killed
					$tag_found	 = false;

					 //Get character ID
					$user_result = mysql_query("SELECT id FROM users WHERE username='$username'");

					//If data exist
					if (mysql_num_rows($user_result) > 0) {
						//Create link
						$user_row = mysql_fetch_array($user_result);
						$username = '@<a href="index.php?id=' . $user_row['id'] . '">' . $username . "<a/> ";
					} else {
						//Just leave it as usual
						$username = '@' . $username . " ";
					}

					//Append
					$wall_post	.= $username;
				} else if ($char == ',' && $tag_found) {
					//Tagging killed
					$tag_found	 = false;

					 //Get character ID
					$user_result = mysql_query("SELECT id FROM users WHERE username='$username'");

					//If data exist
					if (mysql_num_rows($user_result) > 0) {
						//Create link
						$user_row = mysql_fetch_array($user_result);
						$username = '@<a href="index.php?id=' . $user_row['id'] . '">' . $username . "<a/>,";
					} else {
						//Just leave it as usual
						$username = '@' . $username . ",";
					}

					//Append
					$wall_post	.= $username;
				} else {
					//If not tag, just add it to the post
					if ($tag_found) $username .= $char;
					else			$wall_post .= $char;
				}
			}

			//Set as content
			$row['content'] = $wall_post;

			//Add parameter
			$row['comments'] = array();

			//Get comments
			$comment_results	= mysql_query("SELECT * FROM wall_comments WHERE post_id=" . $row['id'] . " ORDER BY time DESC") or die(mysql_error());
			$comment_row		= mysql_fetch_array($comment_results);
			
			$index	= 0;
			$amount	= $comments_in_wall;
	
			while ($comment_row && count($row['comments']) < $amount) {
				//Add to post
				$row['comments'][] = $comment_row;
				$comment_row = mysql_fetch_array($comment_results);
			}

			//Insert to post
			$posts[] = $row;
		}

		//Next
		$row = mysql_fetch_array($result);
		$index++;
	}

	require_once $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/doctype.php';
?>

			<h3 id="content-head"> <?php echo $user['username']?>'s Wall</h3>

			<!-- Wall -->
			<div id="wall">
				<?php
					//Is this your wall?
					if (isset($_SESSION['user']) && $user['id'] == $_SESSION['user']) {
				?>

				<!-- Form -->
				<div id="wall-form">
					<form action=<?php echo "'index.php?id=" . $user['id'] . "'" ?> method="post"> <p>
						<!-- Title input -->
						<label for="input_title" >Title: </label>
						<input type="text"
							   id="input_title"
							   name="title"
						/> <span id="result_title"></span> <br />

						<!-- Content input -->
						<label for="input_post" >Post: </label>
						<textarea cols="32" rows="2"  id="input_post" name="post">Post on your wall!</textarea> <span id="result_post"></span> <br />

						<!-- Button -->
						<input type="submit" value="Post" />
					</p></form>
				</div>
				<?php
					}
				?>

				<!-- Wall posts -->
				<div id="wall-posts">
					<ul class="wall-post">
						<?php
							//For each post
							foreach ($posts as $post) {
						?>

						<!-- Wall Post -->
						<div class='wall-post'>
							<li class="wall-post">

								<!-- Header -->
								<div class='wall-post-header'>
									<div class='wall-post-title'> <?php echo $post['title'] ?> </div>
									<div class='wall-post-time'> <?php echo $post['time'] ?> </div>
									<?php 
										if (isset($_SESSION['user']) && is_numeric($_SESSION['user'])) {
											if ($_SESSION['user'] == $user['id']) {
									?>
									<div class='wall-delete'><a href=<?php echo '"index.php?id=' . $user['id'] . "&del-wall=" . $post['id'] . '"'; ?>>delete</a></div>
									<?php
											}
										}
									?>
								</div>

								<!-- Post -->
								<div class='wall-post-content'>
									<?php echo $post['content'] ?>
								</div>

								<!-- Comments -->
								<div class='wall-comments'>
									<ul id=<?php echo "wall-comments-".$post['id'].""; ?> class="wall-comments">
										<?php
											echo "<a href=\"javascript:getWallComments(".$user['id'].",".$post['id'].");\"><b>[View All Comments]</b></a>&nbsp;&nbsp;&nbsp;\n";
										
											//For each comment
											foreach ($post['comments'] as $comment) {
												//Get poster name
												$result = mysql_query("SELECT * FROM users WHERE id=" . $comment['user_id']) or die(mysql_error());
												$poster = mysql_fetch_array($result);
										?>

										<div class='wall-comment'>
											<li class="wall-comment">
												<!-- Comment header -->
												<div class='wall-comment-header'>
													<div class='wall-comment-user'> <?php echo $poster['username'] ?> </div>
													<div class='wall-comment-time'> <?php echo $comment['time'] ?> </div>
												</div>

												<!-- Comment content -->
												<div class='wall-comment-content'>
													<div class="comment-content"><?php echo $comment['content'] ?></div>
													<?php
														if (isset($_SESSION['user']) && is_numeric($_SESSION['user'])) {
															if ($_SESSION['user'] == $user['id']) {
													?>
													<div class="comment-delete">
														<a href=<?php echo '"index.php?id=' . $user['id'] . "&del-comment=" . $comment['id'] . '"'; ?>>delete</a>
													</div>
													<?php
															}
														}
													?>
												</div>
											</li>
										</div>
										<?php
											}
										?>

										<!-- Comment input -->
										<div class="wall-comment-form">
											<form action=<?php echo "'index.php?id=" . $user['id'] . "'" ?> method="post"> <p>
												<!-- Content input -->
												<textarea cols="32" rows="2" class="input-wall-comment" name="comment">Write comment</textarea>
												<br />

												<!-- Which wall post? -->
												<input type="hidden" name="post_id" value=<?php echo "'" . $post['id'] . "'" ?> />

												<!-- Button -->
												<input type="submit" value="Comment" />
											</p></form>
										</div>
									</ul>
								</div>
							</li>
						</div>
						<?php
							}
						?>
					</ul>
				</div>
			</div>
			
			<!-- Page Navigation -->
			<?php 
			$page_now=1;
		    while($pages) {
		        if ($page_now==$page) {
		            echo "<a href=\"javascript:goto_wall_page(".$page_now.",".$id.");\"><b>[".$page_now."]</b></a>&nbsp;&nbsp;&nbsp;\n";
		        } else {
		            echo "<a href=\"javascript:goto_wall_page(".$page_now.",".$id.");\">".$page_now."</a>&nbsp;&nbsp;&nbsp;\n";
		        }
		        $pages--;
		        $page_now++;
		    }
			?>